package com.ouc.yyi.springbootwebapp.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class RedPackage {

    /**
     * 生成min到max范围的浮点数
     **/
    public static double nextDouble(final double min, final double max) {
        return min + ((max - min) * new Random().nextDouble());
    }

    public static String format(double value) {
        return new java.text.DecimalFormat("0.00").format(value); // 保留两位小数
    }


    //二倍均值法
    public static List<Double> getSmallPackage(double money, int number) {
        List<Double> result = new ArrayList<Double>();
        if (money < 0 && number < 1)
            return null;
        double amount, sum = 0;
        int remainingNumber = number;
        int i = 1;
        while (remainingNumber > 1) {
            amount = nextDouble(0.01, 2 * (money / remainingNumber));
            sum += amount;
            // 将红包信息在该位置记录到redis中
            //System.out.println("第" + i + "个人领取的红包金额为：" + format(amount));
            money -= amount;
            remainingNumber--;
            result.add(amount);
            i++;
        }
        result.add(money);
        System.out.println("第" + i + "个人领取的红包金额为：" + format(money));
        sum += money;
        System.out.println("验证发出的红包总金额为：" + format(sum));

        return result;


    }



    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("这是一段模拟抢红包的代码。");

        int number;
        double money;
        System.out.print("请输入红包总金额：");
        money = sc.nextDouble();
        System.out.print("请输入红包数量：");
        number = sc.nextInt();
        getSmallPackage(money, number);
        //System.out.println(doubleMeanMethod(money,number).toString());
        //也是可以直接输出list的，为了观察方便，我就在循环中输出了，存在list里主要是为了后续方便数据的使用
        System.out.println();



    }
}
